<?php

class SemesterScoreManager_ScoreAllController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        //TODO 綜合表現分數
        $this->view->yearSemester = $this->getRequest()->getPost('yearSemester');
        if ($this->getRequest()->getPost('select-class')) {
            $this->view->selectClass = $this->getRequest()->getPost('select-class');
            $tempArr = explode("-", $this->getRequest()->getPost('yearSemester'));
            $year = (int) $tempArr[0];
            $semester = (int) $tempArr[1];
            $tempArr = explode("-", $this->getRequest()->getPost('select-class'));
            $grade = (int) $tempArr[0];
            $classSn = (int) $tempArr[1];

            // 判斷等第
            $examine = new Semester_Model_SemesterExamine();
            $examineSet = $examine->getExaminSet($year, $semester, $grade);
            $this->view->judgement = $examineSet->judgement;

            $semesterSubject = new Semester_Model_SemesterSubject();
            $semestetSubjectArr = $semesterSubject->getSubject($year, $semester, $grade, $classSn,null, 'Y');
            // 做為排序用
            $subjectRate= array();
            $rateTotal = 0;
            foreach($semestetSubjectArr as $row ) {
                foreach ($row as $val) {
                    $subjectRate[$val['subject_sn']] = $val['rate'];
                    $rateTotal += $val['rate'];
                    //     $subjectCount++;
                }
            }
            $this->view->subjectRate = $subjectRate;

            $this->view->semestetSubjectArr = $semestetSubjectArr ;
            $q = DOctrine_Query::create()
            ->select('a.sn, a.name, b.number AS number,  b.student_id AS student_id,  c.subject_sn, c.score')
            ->from('Student_Model_Student a')
            ->leftJoin('a.Student_Model_StudentSemester b')
            ->leftJoin('a.Student_Model_StudentScoreSemester c')
            ->leftJoin('c.Semester_Model_SemesterSubject d')
            ->where('b.year=?', $year)
            ->andWhere('b.semester=?', $semester)
            ->andWhere('b.grade=?', $grade)
            ->andWhere('b.class_sn=?', $classSn)
            ->andWhere('d.scored=?','Y')
            ->andWHere('b.year=d.year AND b.semester=d.semester AND b.grade=d.grade AND b.class_sn=d.class_sn')
            ->orderBy('b.number');

            $res = $q->fetchArray();

            $this->view->scoreData = $res;

            if ($this->_getParam('act') == 'exportCvs') {
                //
                Zend_Controller_Action_HelperBroker::getStaticHelper('layout')->disableLayout();
                $fileName =  $this->view->yearSemester.'-'.$grade.'-'.$classSn.'.csv';
                $this->getResponse()->setHeader("Pragma","public");
                $this->getResponse()->setHeader("Expires", 0);
                $this->getResponse()->setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
                $this->getResponse()->setHeader("Cache-Control: ","private",false);
                $this->getResponse()->setHeader("Content-Type",'text/csv');
                $this->getResponse()->setHeader("Content-Disposition","attachment; filename=\"".$fileName."\";");
                set_time_limit(0);

                $this->render('cvs');
            }
//            elseif ($this->_getParam('act') == 'friendlyPrint') {
//                $this->view->friendlyPrint = 1;
//                $this->getHelper('layout')->setLayout('print-style');
//            }
        }
    }

    public function testAction()
    {

    }

}

    